// ============================================================================
//
// Copyright (C) 2006-2007 Dengues
//
// Google Group: http://groups.google.com/group/dengues
// QQ Group: 24885404
//
// This library is free software; you can redistribute it and/or
// modify it under the terms of the GNU Lesser General Public
// License as published by the Free Software Foundation; either
// version 2.1 of the License.
//
// This library is distributed in the hope that it will be useful,
// but WITHOUT ANY WARRANTY; without even the implied warranty of
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
// Lesser General Public License for more details.
//
// You should have received a copy of the GNU General Public License
// along with this program; if not, write to the Free Software
// Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
//
// ============================================================================
package org.dengues.core.jdk;

import org.dengues.commons.jdk.CustomClassLoader;
import org.eclipse.core.resources.IProject;
import org.eclipse.core.resources.ResourcesPlugin;
import org.eclipse.jdt.core.IClasspathEntry;
import org.eclipse.jdt.core.IJavaProject;
import org.eclipse.jdt.core.JavaCore;

/**
 * Qiang.Zhang.Adolf@gmail.com class global comment. Detailled comment <br/>
 * 
 * $Id: Dengues.epf 2008-1-7 qiang.zhang $
 * 
 */
public abstract class JavaProjectClassLoader extends CustomClassLoader {

    /**
     * qzhang JavaProjectClassLoader constructor comment.
     * 
     * @param classPath
     */
    public JavaProjectClassLoader() {
        scanPath(getProjectClassPath());
    }

    /**
     * zx Comment method "getProjectClassPath".
     * 
     * @return
     */
    private String getProjectClassPath() {
        String classPath = System.getProperty("java.class.path"); //$NON-NLS-1$
        IProject project = getProject();
        IJavaProject create = JavaCore.create(project);
        try {

            IClasspathEntry[] resolvedClasspath = create.getResolvedClasspath(true);
            for (IClasspathEntry entry : resolvedClasspath) {
                classPath += ";" + entry.getPath().toOSString(); //$NON-NLS-1$
            }
            String workPath = ResourcesPlugin.getWorkspace().getRoot().getFullPath().toOSString();
            String outPath = create.getPath().toOSString() + create.getOutputLocation();
            classPath += ";" + workPath + outPath; //$NON-NLS-1$
        } catch (Exception e) {
        }

        return classPath;
    }

    protected abstract IProject getProject();
}
